package com.example.qxfw.rcyw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.qxfw.common.entity.xt.AgriMoniInfo;
import com.example.qxfw.rcyw.dto.ArgiMoniInfoDto;
import com.example.qxfw.rcyw.mapper.AgriMoniInfoMapper;
import com.example.qxfw.rcyw.service.AgriMoniInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@Service
public class AgriMoniInfoServiceImpl implements AgriMoniInfoService {

    @Autowired
    private AgriMoniInfoMapper agriMoniInfoMapper;

    @Override
    public List<AgriMoniInfo> getMonitorArgi(Integer argiHour) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - argiHour);
        Timestamp warningtime = Timestamp.valueOf(sdf.format(calendar.getTime()));
        LambdaQueryWrapper<AgriMoniInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.gt(AgriMoniInfo::getWarningtime, warningtime).eq(AgriMoniInfo::getIsremove, 0).orderByDesc(AgriMoniInfo::getWarningtime);
        List<AgriMoniInfo> agriMoniInfos = agriMoniInfoMapper.selectList(queryWrapper);
        return agriMoniInfos;
    }

    @Override
    public List<AgriMoniInfo> listByParam(String stationId, String warningTime1, String warningTime2) {
        return agriMoniInfoMapper.selectList(Wrappers.<AgriMoniInfo>lambdaQuery()
                .eq(AgriMoniInfo::getStationid, stationId)
                .between(AgriMoniInfo::getWarningtime, Timestamp.valueOf(warningTime1), Timestamp.valueOf(warningTime2)));
    }

    @Override
    public List<AgriMoniInfo> listByParam1(String stationId, String warningTime1, String warningTime2, String cropName, String growdateName) {
        return agriMoniInfoMapper.selectList(Wrappers.<AgriMoniInfo>lambdaQuery()
                .eq(AgriMoniInfo::getStationid, stationId)
                .between(AgriMoniInfo::getWarningtime, Timestamp.valueOf(warningTime1), Timestamp.valueOf(warningTime2))
                .eq(StringUtils.isNotBlank(cropName) && !cropName.equals("全部"), AgriMoniInfo::getCropname, cropName)
                .eq(StringUtils.isNotBlank(growdateName) && !growdateName.equals("全部"), AgriMoniInfo::getGrowdatename, growdateName));
    }

    @Override
    public Integer updateMonitorArgi(String ids) {
        int count = 0;
        String[] idarr = ids.split(",");
        for (int k = 0; k < idarr.length; k++) {
            AgriMoniInfo info = new AgriMoniInfo();
            info.setIsremove((short) 1);
            LambdaUpdateWrapper<AgriMoniInfo> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(AgriMoniInfo::getId, Integer.parseInt(idarr[k]));
            count += agriMoniInfoMapper.update(info, updateWrapper);
        }
        return count;
    }

    @Override
    public List<ArgiMoniInfoDto> getMonitorArgiXwx(Integer agriHour) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - agriHour);
        String warningTime = sdf.format(calendar.getTime());
        //Timestamp timestamp = Timestamp.valueOf(warningTime);
        return agriMoniInfoMapper.selectArgiXwxJk(warningTime);
    }

    @Override
    public List<AgriMoniInfo> getGrowDateName(String cropName) {
        return agriMoniInfoMapper.selectList(Wrappers.<AgriMoniInfo>query()
                .select(" distinct growdatename ")
                .lambda()
                .eq(AgriMoniInfo::getCropname, cropName));
    }
}
